package com.controller.admin;

import com.domain.Blog;
import com.domain.BlogType;
import com.domain.Blogger;
import com.domain.Link;
import com.service.BlogService;
import com.service.BlogTypeService;
import com.service.BloggerService;
import com.service.LinkService;
import com.util.Const;
import com.util.ResponseUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin/system")
public class SystemAdminController {
    @Resource
    private BlogTypeService blogTypeService;
    @Resource
    private BlogService blogService;
    @Resource
    private LinkService linkService;
    @Resource
    private BloggerService bloggerService;
    @RequestMapping("/refreshSystem")
    public String refreshSystem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取上下文
        ServletContext servletContext= ContextLoader.getCurrentWebApplicationContext().getServletContext();
        List<BlogType> list =blogTypeService.countList();
        servletContext.setAttribute(Const.BLOG_TYPE_COUNT_LIST,list);
        //博主信息
        Blogger blogger=bloggerService.find();
        blogger.setPassword(null);
        servletContext.setAttribute(Const.BLOGGER,blogger);
        //友情链接
        List<Link> linkList=linkService.list(null);
        servletContext.setAttribute(Const.Link_LIST,linkList);
        //博客数量按年月日分类
        List<Blog> blogs=blogService.countList();
        servletContext.setAttribute(Const.BLOG_COUNT_LIST,blogs);
        JSONObject result=new JSONObject();
        result.put("success",Boolean.TRUE);
        ResponseUtil.write(response,result);
        return null;
    }

}
